package com.outdoor.model.dto.response;

import com.outdoor.model.entity.Trip;
import com.outdoor.model.enums.ActivityType;
import com.outdoor.model.enums.TripStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 行程响应DTO
 * 
 * @author OutdoorTeam
 * @date 2025-05-18
 */
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class TripResponse {

    private Long id;
    private String name;
    private ActivityType activityType;
    private String location;
    private LocalDate date;
    private String description;
    private TripStatus status;
    
    private UserInfo organizer;
    private List<String> photos;
    private Long teamId;
    private Integer memberCount;
    
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserInfo {
        private Long id;
        private String name;
    }
    
    /**
     * 从实体转换为DTO (基础版本，不含关联信息)
     * 
     * @param trip 行程实体
     * @return 行程响应DTO
     */
    public static TripResponse fromEntity(Trip trip) {
        return fromEntity(trip, null);
    }
    
    /**
     * 从实体转换为DTO (支持传入组织者名称)
     * 
     * @param trip 行程实体
     * @param organizerName 组织者名称，可为null
     * @return 行程响应DTO
     */
    public static TripResponse fromEntity(Trip trip, String organizerName) {
        return TripResponse.builder()
                .id(trip.getId())
                .name(trip.getName())
                .activityType(trip.getActivityType())
                .location(trip.getLocation())
                .date(trip.getDate())
                .description(trip.getDescription())
                .status(TripStatus.valueOf(trip.getStatus()))
                .organizer(UserInfo.builder()
                        .id(trip.getOrganizerId())
                        .name(organizerName != null ? organizerName : "")
                        .build())
                .photos(stringToPhotoList(trip.getPhotos()))
                .teamId(trip.getTeamId())
                .memberCount(0) // 需要通过TeamMemberMapper查询成员数量
                .build();
    }
    
    /**
     * 使用额外信息补充TripResponse
     *
     * @param trip 行程实体
     * @param organizerName 组织者名称
     * @param memberCount 成员数量
     * @return 完整的行程响应DTO
     */
    public static TripResponse fromEntityWithDetails(Trip trip, String organizerName, int memberCount) {
        return fromEntity(trip, organizerName).toBuilder()
                .memberCount(memberCount)
                .build();
    }
    
    /**
     * 将照片字符串转为照片列表
     *
     * @param photosStr 照片字符串，逗号分隔
     * @return 照片URL列表
     */
    private static List<String> stringToPhotoList(String photosStr) {
        if (photosStr == null || photosStr.isEmpty()) {
            return Collections.emptyList();
        }
        List<String> result = new ArrayList<>();
        for (String photo : photosStr.split(",")) {
            result.add(photo.trim());
        }
        return result;
    }
    
    /**
     * 从实体集合转换为DTO列表
     * 
     * @param trips 行程实体集合
     * @return 行程响应DTO列表
     */
    public static List<TripResponse> fromEntityList(List<Trip> trips) {
        if (trips == null) {
            return Collections.emptyList();
        }
        return trips.stream()
                .map(TripResponse::fromEntity)
                .toList();
    }
} 